<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace app\system\api;

use app\system\model\Member;
use app\common\api\BaseApi;
use app\system\model\UserLog;

class MemberApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "system/MemberApi/add";
    /**
     * 保存
     */
    const SAVE = "system/MemberApi/save";
    /**
     * add by 周舟 hzboye010@163.com
     * add begin
     */
    const SET_DEC = "system/MemberApi/setDec";
    const SET_INC = "system/MemberApi/setInc";
    const RAISE_SCORE = "system/MemberApi/raiseScore";
    const DECREASE_SCORE = "system/MemberApi/decreaseScore";
    /**
     * add end
     */

    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "system/MemberApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "system/MemberApi/delete";

    /**
     * 查询
     */
    const QUERY = "system/MemberApi/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "system/MemberApi/getInfoWithCache";
    /**
     *
     */
    const QUERY_BY_GROUP = "system/MemberApi/queryByGroup";
    
    const QUERY_NO_PAGING = "system/MemberApi/queryNoPaging";

    const PRETEND_DELETE = "system/MemberApi/pretendDelete";

    const QUERY_WITH_GROUP = "system/MemberApi/queryWithGroup";

    //初始化
    protected function _init(){
        $this->model = new Member();
    }

    public function getInfoWithCache($map,$order=false,$field=false){
        if(isset($map['uid'])){
            $uid=$map['uid'];
            $cacheKey = 'minfo-'.$uid;
            $cache    = json_decode(S($cacheKey),true);
            if(!is_null($cache)) return $this->apiReturnSuc($cache);
            $r = $this->getInfo($map,$order,$field);
            if(!$r['status']) return $this->apiReturnErr($r['info']);
            S($cacheKey,json_encode($r['info']));
        }else{
            $r = $this->getInfo($map,$order,$field);
        }
        return $r;
    }

    public function raiseScore($uid,$score){
        $uid   = intval($uid);
        $score = abs(intval($score));
        $map   = ['uid' => $uid];
        $result = $this -> model -> where($map) -> setInc('score',$score);
        if($result === false){
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }else{
            return $this -> apiReturnSuc($result);
        }

    }

    /**
     * 减去用户积分
     * @param $uid
     * @param $score 输入为正数
     * @param $flag  输入为正数
     */
    public function decreaseScore($uid,$score,$flag=false){
        $uid   = intval($uid);
        $score = abs(intval($score));
        $map   = ['uid' => $uid];
        if($flag){
            $user_score = $this -> model ->where($map) ->getField('score');
            if(false === $user_score) return $this -> apiReturnErr($this -> model -> getDbError());
            if($user_score < $score)  return $this -> apiReturnErr(UserLog::SCORE.'不足');
        }
        $result = $this -> model -> where($map) -> setDec('score',$score);
        if(false === $result){
            return $this -> apiReturnErr($this -> model -> getDbError());
        }else{
            return $this -> apiReturnSuc($result);
        }
    }
    /**
     * 根据id保存数据
     */
    public function saveByID($uid, $entity) {
        unset($entity['id']);

        return $this -> save(array('uid' => $uid), $entity);
    }

    /**
     * 与 queryWithGroup 类似，这个是较早版本
     * @param $map
     * @param $page
     * @return array
     */
    public function queryByGroup($map,$page)
    {
        $result = $this->model->queryByGroup($map, $page);
        if ($result === false) {
            return $this->apiReturnErr($this->model->getDbError());
        } else {
            return $this->apiReturnSuc($result);
        }
    }

    public function queryWithGroup($nicknameOrUid = '',$add_uid=0,$group_id=0, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false){

        $map = array();
        if(!empty($nicknameOrUid)){
            $map['nickname']=array('like','%'.$nicknameOrUid.'%');
        }
        $map['add_uid'] = $add_uid;
        $map['group_id'] = $group_id;
        $list = $this->model->alias("m")->join(" __AUTH_GROUP_ACCESS__ as  ac on m.uid = ac.uid  ","RIGHT")
            ->where($map)
            -> page($page['curpage'] . ',' . $page['size']) ->select();

        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));

    }

}